﻿using System.Web.Mvc;
using LuKou.Member.Domains;
using LuKou.Shopspace.Domains;
using LuKou.Web.Filter;
using LuKou.Web.UserAuth;
using LuKou.Web.BLL;
using LuKou.Web.BLL.Service;

namespace LuKou.Web.Controllers.Base
{
    /// <summary>
    /// 网站控制器基类
    /// </summary>
    
    [GZip]
    [WebHandleError]
    public class BaseController : Controller
    {
        private IMembership membership = null;

        /// <summary>
        /// 用户登录后的会话信息
        /// </summary>
        public UserSession UserSession
        {
            get
            {
                if (this.Session["user_session"] == null) return null;
                return (UserSession)this.Session["user_session"];
            }

            protected set
            {
                this.Session.Add("user_session", value);
            }
        }

        /// <summary>
        /// 用户权限接口
        /// </summary>
        public virtual IMembership Membership
        {
            get
            {
                if (membership == null)
                {
                    membership = new UserMembership();
                    membership.Read();
                }
                return membership;
            }
        }

        /// <summary>
        /// 转跳到网站首页
        /// </summary>
        /// <returns></returns>
        public virtual RedirectToRouteResult RedirectIndex()
        {
            return RedirectToAction("Index", "Home");
        }

        protected override void OnAuthorization(AuthorizationContext filterContext)
        {
            this.Membership.Read();
            base.OnAuthorization(filterContext);
        }

        protected override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            filterContext.HttpContext.Response.AddHeader("LuKouWeb-Version", "1.0");
            filterContext.HttpContext.Response.AddHeader("Developer", "Arcai,QQ:55780844|Nathan,QQ:393051825");
            base.OnActionExecuting(filterContext);
        }

        protected override void OnActionExecuted(ActionExecutedContext filterContext)
        {
            this.ViewData["UserSession"] = this.UserSession;
            //this.ViewData["UserInfo"] = this.Membership.User;

            base.OnActionExecuted(filterContext);
        }

        protected override void Dispose(bool disposing)
        {
            base.Dispose(disposing);
        }
    }


    public static class UserSessionExt
    {
        /// <summary>
        /// 获取用户的店铺
        /// </summary>
        /// <param name="session"></param>
        /// <returns></returns>
        public static Shop GetShop(this UserSession session)
        {
            return ShopService.GetShopByUserID(session.UserID);
        }

        /// <summary>
        /// 获取当前的用户信息
        /// </summary>
        /// <param name="session"></param>
        /// <returns></returns>
        public static User GetUserInfo(this UserSession session)
        {
            return UserService.GetUserInfoById(session.UserID);
        }
    }
}
